Mostly Harmless
Legacy:Object Oriented Programming Overview
This tutorial assumes you know a little bit about programming already. Maybe you've written a bit of javascript or some perl, or some BASIC programs. Here we explain the additional concepts you need to program in an Object Oriented style.
Writing UnrealScript is not possible without writing classes and using objects. Every player, vehicle, weapon, pickup, and light is an object. Even abstract things like the game rules and the scoreboard are objects. Every object belongs to a Class, which describes what data and functions are applicable to that object. For example, each player has an AssaultRifle. These are all of the same Class: every AssaultRifle has primary and secondary fire, and has a number of bullets and a number of grenades; but they are separate Objects (because each AssaultRifle in a game has a different number of bullets and grenades remaining).
- class
- the 'blueprint' for objects
- object
- an actual thing in the world
There are many ways to think of classes and objects: you can imagine a class as a "recipe" or a "blueprint" for making things. It defines properties that every object of this class will have (even if the values may differ), and functions (or methods) that every object of this class can perform.
Contents
Creating a Class[edit]
Say you want to create a Pizza. A Pizza is a meal, so:
class Pizza extends Meal;
A Pizza is generally divided into smaller parts:
var int NumberOfParts=1;
Also, a pizza remembers whether it has tomato sauce or cheese on it:
var bool bHasSauce; var bool bHasCheese;
Those are all properties that exist on any pizza you may create.
Now for some methods:
You can slice the pizza:
function Slice(pieces) { NumberOfParts=pieces }
You can add sauce, or eat the sauce, you can add cheese, or eat the cheese:
function AddSauce() { bHasSauce = true; } function EatSauce() { bHasSauce = false; } function AddCheese() { bHasCheese = true; } function EatCheese() { bHasCheese = false; }
You can eat parts of the pizza:
function EatParts(int number) { NumberOfParts -= number; if(NumberOfParts <= 0) Destroy(); }
Notice that if the pizza object has 0 parts left, it no longer exists and must be destroyed. If you really wanted to be polite, you might want the EatParts function to return a value to tell the person trying to eat it if they have been successful or not, for instance trying to eat four parts when there are only three left.
Where's My Pizza?[edit]
Mind you, what we've been doing up to here is just describing what a pizza looks like and how to create (and eat) it. It's like you're writing a recipe (that's your UnrealScript class).
You still don't have a pizza to eat right now, even though you perfectly know how to eat it and what it would look like. In real life, you'd have to gather kitchen utensils and pizza ingredients and bake one; in UnrealScript, you can just wave your magic wand and spawn one:
// Now that I know what a pizza looks like, I'm hungry and want to eat one. local Pizza MyPizza; // That's where I'm going to hold the pizza. Like a dish. MyPizza = Spawn(class 'Pizza'); // Hooray! Pizza's ready! MyPizza.Slice(8); // Cut into 8 parts MyPizza.EatParts(2); // Now I'm eating two parts of my pizza.
Subclassing[edit]
Now, you want a better Pizza, with mushrooms and bacon and egg. This is an enhanced pizza, so:
class BetterPizza extends Pizza;
This subclass inherits all the variables (NumberOfParts, bHasCheese, etc) from the Pizza class. So it already has cheese and it has sauce, but this sauce doesn't include egg and mushrooms, so we have to change this:
var bool bHasMushrooms; var bool bHasEgg; function AddSauce() { bHasEgg = true; bHasMushrooms = true; Super.AddSauce(); }
Hold it! What's this "Super" thing?
It is a way to call the original Pizza's AddSauce function. This way, we don't have to re-code the Sauce Management :D Same goes for EatSauce:
function EatSauce() { bHasEgg = false; bHasMushrooms = false; Super.EatSauce(); }
The BetterPizza also inherited the functions to handle the parts. It is a complete, functional Pizza :)
Now that you've mastered the use of a single object, you can begin to work with multiple objects that interact with each other. This is covered in the next part of this this tutorial.
What next[edit]
- Using objects – part two of this tutorial
Related Topics[edit]
- UnrealScript Language Reference – Tim Sweeney's introduction to UnrealScript
- Object states in UnrealScript
- Unreal Object Oriented Programming – Introduction to object orientation and modding Unreal in general
- Peppers And Pepper Grinders – Another analogy to help more easily understand object oriented programming
Discussion[edit]
Sweavo: I'd like to see the example replaced by one you can actually try in-game.
MythOpus: I think this page is more for the theory of Object Oriented Programming, rather than seeing how it works, if that makes sense. I think an actual working example that one would be able to try would be pointless at this stage as it would require someone to go through the steps of setting up a package, which many may not know how to do at this point.
Sweavo: true enough. But a crash course in OO programming is a bit off-topic for an unreal wiki, no? I take your point about the package though.
MythOpus: I guess it is slightly off-topic, but not entirely as UnrealScript is an Object Oriented language.